package com.jiumi.order.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiumi.baseconfig.domain.BaseCourseInfo;
import com.jiumi.baseconfig.domain.BaseMeal;
import com.jiumi.baseconfig.domain.BaseUser;
import com.jiumi.baseconfig.mapper.BaseCourseInfoMapper;
import com.jiumi.baseconfig.mapper.BaseMealMapper;
import com.jiumi.baseconfig.service.IBaseUserService;
import com.jiumi.common.utils.DateUtils;
import com.jiumi.common.utils.SecurityUtils;
import com.jiumi.common.utils.StringUtils;
import com.jiumi.order.domain.BaseOrderDetail;
import com.jiumi.order.mapper.BaseOrderDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jiumi.order.mapper.BaseOrderInfoMapper;
import com.jiumi.order.domain.BaseOrderInfo;
import com.jiumi.order.service.IBaseOrderInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单信息Service业务层处理
 *
 * @author jiumi
 * @date 2023-05-24
 */
@Service
public class BaseOrderInfoServiceImpl extends ServiceImpl<BaseOrderInfoMapper, BaseOrderInfo> implements IBaseOrderInfoService {
    @Autowired
    private BaseOrderInfoMapper baseOrderInfoMapper;

    @Autowired
    private IBaseUserService baseUserService;

    @Autowired
    private BaseOrderDetailMapper baseOrderDetailMapper;

    @Autowired
    private BaseCourseInfoMapper courseInfoMapper;

    @Autowired
    private BaseMealMapper baseMealMapper;
    /**
     * 查询订单信息
     *
     * @param id 订单信息主键
     * @return 订单信息
     */
    @Override
    public BaseOrderInfo selectBaseOrderInfoById(Long id) {
        BaseOrderInfo baseOrderInfo = baseOrderInfoMapper.selectBaseOrderInfoById(id);
        List<BaseOrderDetail> baseOrderDetailList = baseOrderInfo.getBaseOrderDetailList();
        Long[] ids = baseOrderDetailList.stream().map(BaseOrderDetail::getCourseId).toArray(Long[]::new);
        baseOrderInfo.setCourseIds(ids);
        return baseOrderInfo;
    }

    /**
     * 查询订单信息列表
     *
     * @param baseOrderInfo 订单信息
     * @return 订单信息
     */
    @Override
    public List<BaseOrderInfo> selectBaseOrderInfoList(BaseOrderInfo baseOrderInfo) {
        return baseOrderInfoMapper.selectBaseOrderInfoList(baseOrderInfo);
    }


    /**
     * 新增订单信息
     *
     * @param baseOrderInfo 订单信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBaseOrderInfo(BaseOrderInfo baseOrderInfo) {
        baseOrderInfo.setCreateTime(DateUtils.getNowDate());
        setOrderCourseList(baseOrderInfo);
        updatePrice(baseOrderInfo);
        int rows = baseOrderInfoMapper.insertBaseOrderInfo(baseOrderInfo);

        insertBaseOrderDetail(baseOrderInfo);
        return rows;
    }

    private void setOrderCourseList(BaseOrderInfo baseOrderInfo) {
        Long[] courseIds = baseOrderInfo.getCourseIds();

        if (null != courseIds && courseIds.length > 0) {
            BaseCourseInfo course = new BaseCourseInfo();
            course.setIds(courseIds);
            List<BaseCourseInfo> baseCourseInfos = courseInfoMapper.selectBaseCourseInfoList(course);

            List<BaseOrderDetail> details = new ArrayList<>();
            for (BaseCourseInfo c : baseCourseInfos) {
                BaseOrderDetail dt = new BaseOrderDetail();
                dt.setOrderId(baseOrderInfo.getId());
                dt.setCourseId(c.getId());
                dt.setCourseName(c.getCourseName());
                dt.setCoverImage(c.getCoverImage());
                dt.setTeacherId(c.getTeacherId());
                dt.setTeacherName(c.getTeacherName());
                dt.setCourseType(c.getCourseType());
                dt.setCourseTypeId(c.getCourseTypeId());
                dt.setIntro(c.getIntro());
                dt.setPrice(c.getPrice());
                dt.setStartTime(c.getStartTime());
                details.add(dt);
            }
            baseOrderInfo.setBaseOrderDetailList(details);
        }
    }

    private void updatePrice(BaseOrderInfo baseOrderInfo) {
        List<BaseOrderDetail> baseOrderDetailList = baseOrderInfo.getBaseOrderDetailList();

        if (baseOrderDetailList.size() > 0) {
            BigDecimal reduce = baseOrderDetailList.stream().map(BaseOrderDetail::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
            baseOrderInfo.setOrderAmount(reduce);
        } else {
            baseOrderInfo.setOrderAmount(new BigDecimal("0"));
        }
        if (StringUtils.isNull(baseOrderInfo.getId())) {
            Long userId = baseOrderInfo.getUserId();
            BaseUser baseUser = baseUserService.selectBaseUserByUserId(userId);
            if (null != baseUser) {
                baseOrderInfo.setUserName(baseUser.getUserName());
                baseOrderInfo.setPhonenumber(baseUser.getPhonenumber());

            }
            baseOrderInfo.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
            baseOrderInfo.setOrderNumber(StringUtils.getRandomStr());
        }
        BaseMeal baseMeal = baseMealMapper.selectBaseMealById(baseOrderInfo.getMealId());
        if(null != baseMeal)
        {
            baseOrderInfo.setMealName(baseMeal.getMealName());
        }

    }


    /**
     * 修改订单信息
     *
     * @param baseOrderInfo 订单信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBaseOrderInfo(BaseOrderInfo baseOrderInfo) {
        baseOrderInfo.setUpdateTime(DateUtils.getNowDate());
        baseOrderInfoMapper.deleteBaseOrderDetailByOrderId(baseOrderInfo.getId());
        setOrderCourseList(baseOrderInfo);
        updatePrice(baseOrderInfo);
        insertBaseOrderDetail(baseOrderInfo);
        return baseOrderInfoMapper.updateBaseOrderInfo(baseOrderInfo);
    }

    /**
     * 批量删除订单信息
     *
     * @param ids 需要删除的订单信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBaseOrderInfoByIds(Long[] ids) {
        baseOrderInfoMapper.deleteBaseOrderDetailByOrderIds(ids);
        return baseOrderInfoMapper.deleteBaseOrderInfoByIds(ids);
    }


    /**
     * 删除订单信息信息
     *
     * @param id 订单信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBaseOrderInfoById(Long id) {
        baseOrderInfoMapper.deleteBaseOrderDetailByOrderId(id);
        return baseOrderInfoMapper.deleteBaseOrderInfoById(id);
    }

    @Override
    public List<BaseOrderInfo> selectUserOrderInfoList(BaseOrderInfo baseOrderInfo) {
        List<BaseOrderInfo> orderList = baseOrderInfoMapper.selectUserOrderInfoList(baseOrderInfo);
        orderList.stream().forEach(order -> {
            BaseOrderDetail param = new BaseOrderDetail();
            param.setOrderId(order.getId());
            List<BaseOrderDetail> detailList = baseOrderDetailMapper.selectBaseOrderDetailList(param);
            order.setBaseOrderDetailList(detailList);
        });
        return orderList;
    }

    /**
     * 新增订单详情信息
     *
     * @param baseOrderInfo 订单信息对象
     */
    public void insertBaseOrderDetail(BaseOrderInfo baseOrderInfo) {
        List<BaseOrderDetail> baseOrderDetailList = baseOrderInfo.getBaseOrderDetailList();
        Long id = baseOrderInfo.getId();
        if (StringUtils.isNotNull(baseOrderDetailList)) {
            List<BaseOrderDetail> list = new ArrayList<BaseOrderDetail>();
            for (BaseOrderDetail baseOrderDetail : baseOrderDetailList) {
                baseOrderDetail.setOrderId(id);
                list.add(baseOrderDetail);
            }
            if (list.size() > 0) {
                baseOrderInfoMapper.batchBaseOrderDetail(list);
            }
        }
    }
}
